/*
 * Copyright (c) 2007 T.Cogghe, D.Leroux, S.Lévignac, S.Rives
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "IHM/gtk.hh"
#include "IHM/IHM.hh"
#include "IHM/editAstres.hh"
#include "IHM/fenetreFichiers.hh"
#include "IHM/fenetreAstres.hh"
#include "IHM/fenetreDark.hh"
#include "IHM/fenetreTraitement.hh"
#include "IHM/fenetreFinale.hh"
#include "IHM/dialogueFichiers.hh"
#include "IHM/dialogueRepertoire.hh"
#include "IHM/dialogueQuitter.hh"
#include "IHM/dialogueSauver.hh"
#include "IHM/dialogueErreurForme.hh"
#include "IHM/dialogueErreurMatrice.hh"

#include "superpaws_xml.hh"

#include "ImageRecadree.hh"

Gtk::Main*m=NULL;
Glib::RefPtr<Gnome::Glade::Xml> refGlade;

IHM* IHM::env=NULL;

IHM::IHM(int& argc,char**&argv) {

	if(env!=NULL) {
		cerr<<"Une autre instance de Superpaws est déjà "
			"en train de tourner dans ce processus ! "
			"exécution avortée."<<endl;
		throw new exception();
	}
	env=this;

	edAs=NULL;
	iFin=NULL;

	/* bootstrap */

	m=new Gtk::Main(argc,argv);
	Glib::thread_init(0);
	refGlade = Gnome::Glade::Xml::create_from_buffer(
					superpaws_xml,strlen(superpaws_xml));

	getWD(refGlade,fFic,"fenetreFichiers");
	getWD(refGlade,fAst,"fenetreAstres");
	getWD(refGlade,fDark,"fenetreDark");
	getWD(refGlade,fTra,"fenetreTraitement");
	getWD(refGlade,fFin,"fenetreFinale");

	getWD(refGlade,dFic,"dlgListeFichiers");
	getWD(refGlade,dRep,"dlgRepertoire");
	getWD(refGlade,dSauv,"dlgSauvegarde");
	getWD(refGlade,dQuit,"dlgQuitter");
	getWD(refGlade,dErrMatrice,"dlgErreurMatrice");
	getWD(refGlade,dErrForme,"dlgErreurForme");

	/* fin du bootstrap */

	int a=1;
	std::string file;
	while(a<argc) {
		if(!strcmp(argv[a],"-l")) {
			/* tous les arguments suivants sont des fichiers à ajouter
			 */
			++a;
			while(a<argc) {
				file=argv[a++];
				ajoutListe(file);
			}
		} else {
			++a;
		}
	}
}


void IHM::run() {
	iFin=NULL;
	fFic->show();
	m->run();
}

IHM::~IHM() {
	if(edAs) delete edAs;
	if(iFin) delete iFin;
	env=NULL;
	delete m;
}


void IHM::traitementAnnule() {
}

void IHM::traitementFini() {
	fTra->proxy_fini();
	throw Glib::Thread::Exit();
}

void IHM::proxyTraitement() {
	traitement();

//*
//	g->cursConn.block(true);
//	g->cursConn.block(false);
//*/
}



void IHM::demarreTraitement() {
	IHM::env->fAst->hide();

	/* initialiser l'image finale */
	if(IHM::env->iFin) {
		delete IHM::env->iFin;
	}
	/* prépare un nouveau pixbuf pour recevoir le résultat */
	Image tmp(IHM::env->image(0)->copiePixbuf());
	IHM::env->iFin=new ImageFinale(tmp);
	IHM::env->fFin->imgFinale->set(*IHM::env->iFin);

	IHM::env->fTra->filTraitement = Glib::Thread::create(
				sigc::mem_fun(*IHM::env,&IHM::proxyTraitement),
				0,false,true, Glib::THREAD_PRIORITY_NORMAL);
	IHM::env->fTra->show();
}


void IHM::initListeImages(std::vector<std::string>fichiers) {
	std::vector<std::string>::iterator i=fichiers.begin(),j=fichiers.end();	
	images.clear();
	/* la première image n'a pas à être recadrée */
	images.push_back(new Image(*i));
	++i;
	while(i!=j) {
		/* par contre les suivantes, elles, si */
		images.push_back(new ImageRecadree(*i));
		++i;
	}
}


void IHM::ajoutListe(std::string fic) {
	std::string date_exif("TODO");
	std::string date_fic("TODO");
	Gtk::TreeRow i = *(fFic->listeFic->store->append());
	Glib::ustring uf(fic), ude(date_exif), udf(date_fic);
	i[fFic->listeFic->tmdl.fichier]=uf;
	i[fFic->listeFic->tmdl.date_exif]=ude;
	i[fFic->listeFic->tmdl.date_fic]=udf;
}




	/* manipulation de la liste de fichiers à traiter */
unsigned int IHM::nbImages() const {
	return images.size();
}

Image* IHM::image(const unsigned int n) const {
	return images[n];
}

	/* manipulation de l'image finale */
Image* IHM::imageFinale(const Image* i) {
	return iFin;
}

	/* affichage de l'état pendant le traitement et
	 * gestion de la barre d'avancement
	 */
//void IHM::etatAjouterTexte(std::string&s) {
//	cout<<"TODO : IHM::etatAjouterTexte(std::string&) :: "<<s<<endl;
	//fTra->etat->push(Glib::ustring(s));
//	fTra->etat->get_buffer()->insert_at_cursor(Glib::ustring(s));
//	fTra->etat->queue_draw();
//}
//
//
	/* mise à jour de la barre de progression (valeur entre 0 et 1) */
void IHM::majProgression(std::string& dump,double c) {
	//g->barreProg->set_fraction(c);
	/* donne un peu d'air à Gtk */
	prog=c;
	texte=dump;
	fTra->proxy_majProg();
	Glib::usleep(1);
	Glib::Thread::yield();
}

int IHM::traitement() { return 0; }



